package com.skyc.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.security.KeyStore.PrivateKeyEntry;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

import javax.lang.model.type.PrimitiveType;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import javax.sql.rowset.CachedRowSet;

import com.sun.rowset.CachedRowSetImpl;



public class JDBCUtil {
//	private static String user;
//	private static String passWord;
//	private static String url;
//	private static String driver;
	
	public static DataSource ds;
	
	
	private  Connection conn = null;
	private  ResultSet rs = null;
	private  PreparedStatement ps = null;
	private static JDBCUtil util;
	private JDBCUtil(){
		
	}
	public synchronized static JDBCUtil getJDBCUtil(){
		if(util!=null){
			return util;
		}
		return util=new JDBCUtil();
	}
	
	static{
		try {
			Context ctx=new InitialContext();
			ds=(DataSource) ctx.lookup("java:comp/env/mysky");
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
//		Properties pro = new Properties();
//		ClassLoader classLoader = JDBCUtil.class.getClassLoader();
//		URL rs = classLoader.getResource("pro.properties");
//		String path = rs.getPath();
//		FileReader fr = null;
//		try {
//			fr = new FileReader(new File(path));
//			pro.load(fr);
//		} catch (FileNotFoundException e1) {
//			e1.printStackTrace();
//		} catch (IOException e1) {
//			e1.printStackTrace();
//		} finally {
//			if (fr != null) {
//				try {
//					fr.close();
//				} catch (IOException e) {
//					e.printStackTrace();
//				}
//			}
//
//		}
//		url = pro.getProperty("url");
//		user = pro.getProperty("user");
//		passWord = pro.getProperty("passWord");
//		driver = pro.getProperty("driver");
//		try {
//			Class.forName(driver);
//		} catch (ClassNotFoundException e) {
//			e.printStackTrace();
//		}
	}

	public void getConnection() {
		try {
			conn=ds.getConnection();
			
//			conn = DriverManager.getConnection(url, user, passWord);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void closeAll() {
		
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (ps != null) {
			try {
				ps.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
	}

	public boolean update(String sql, Object... obj) {
		getConnection();
		try {
			ps = conn.prepareStatement(sql);
			if (obj != null) {
				for (int i = 0; i < obj.length; i++) {
					ps.setObject(i + 1, obj[i]);
				}
			}
			return ps.executeUpdate()>0;
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			closeAll();
		}
		return false;
	}
	public CachedRowSet query(String sql, Object... obj){
		getConnection();
		CachedRowSet rowset=null;
		try {
			
			ps = conn.prepareStatement(sql);
			if (obj != null) {
				for (int i = 0; i < obj.length; i++) {
					ps.setObject(i + 1, obj[i]);
				}
			}
			rs=ps.executeQuery();
			rowset=new CachedRowSetImpl();
			rowset.populate(rs);
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			closeAll();
		}
		return rowset;
		
	}
	
	public void closeCacheRowSet(CachedRowSet rs) {
		try {
			rs.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
